package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class MyJwtUtil {

    public static final String KEY_STRING = "aaaaaaaazzzzzzzzssssssssxxxxxxxx";

    /**
     * 签名秘钥
     */
    public static final Key SING_KEY = Keys.hmacShaKeyFor(KEY_STRING.getBytes());

    public static final long EXPIRE = 1000 * 60 * 60 * 12;

    public static String create(Map<String, Object> claimsMap) {

        Date now = new Date();
        return Jwts.builder().signWith(SING_KEY)    //签名秘钥
                .setClaims(claimsMap)   //payload 声明
                .setIssuedAt(now)   //token 签发时间
                .setExpiration(new Date(now.getTime() + EXPIRE))    //token 过期时间
                .compact();
    }

    /**
     * 解析 token
     *
     * @param token
     * @return
     */
    public static Map<String, Object> parse(String token) {
        return Jwts.parserBuilder().setSigningKey(SING_KEY)
                .build().parseClaimsJws(token)
                .getBody();
    }

}
